package com.example.springbootdemo.controller;

import com.example.springbootdemo.bean.QuartzJobProperties;
import com.example.springbootdemo.bean.dto.UserDTO;
import com.example.springbootdemo.bean.entity.User;
import com.example.springbootdemo.bean.response.ResponseInfo;
import com.example.springbootdemo.mapstruct.UserConvert;
import com.example.springbootdemo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hh
 * @since 2022-01-19
 */
@Slf4j
@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/api")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private QuartzJobProperties quartzJobProperties;

    @Value("${jasypt.encryptor.password}")
    private String salt;

    /**
     * 获取用户信息
     * @return ResponseInfo
     */
    @ApiOperation("查询用户的接口")
    @GetMapping("user")
    // @PreAuthorize("hasRole('admin')")
    public ResponseInfo getUser(@RequestParam(value = "userName", required = false) String userName) {
        log.info("获取到的盐值是：[{}]", salt);
        log.info("获取到job1:{}", quartzJobProperties.getJob1());
        log.info("获取到job2:{}", quartzJobProperties.getJob2());
        List<User> users = userService.getUserByUserName(userName);
        if (Objects.isNull(userName)){
            return ResponseInfo.success(users);
        }
        if (users.size() == 1){
            return ResponseInfo.success(users.get(0));
        }
        return ResponseInfo.success(users);
    }


    /**
     * 保存用户接口
     * @param userDTO userDTO
     * @return ResponseInfo
     */
    @ApiOperation("保存用户接口")
    @PostMapping("user")
    public ResponseInfo saveUser(@Validated UserDTO userDTO){
        User user = UserConvert.USER_CONVERT.userDTOToUser(userDTO);

        // 加密密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        boolean save = userService.save(user);
        if (save){
            return ResponseInfo.success();
        }
        return ResponseInfo.fail();
    }

    /**
     * 修改用户接口
     * @param user user
     * @return ResponseInfo
     */
    @ApiOperation("修改用户接口")
    @PutMapping("user")
    public ResponseInfo updateUser(User user){
        boolean update = userService.updateById(user);
        if (update){
            return ResponseInfo.success();
        }
        return ResponseInfo.fail();
    }

    /**
     * 删除用户接口
     * @param id 用户id
     * @return ResponseInfo
     */
    @ApiOperation("删除用户")
    @DeleteMapping("user")
    public ResponseInfo deleteUser(Integer id){
        boolean remove = userService.removeById(id);
        if (remove){
            return ResponseInfo.success();
        }
        return ResponseInfo.fail();
    }


}
